package com.mach.platform.api.service;

import com.mach.platform.api.domain.Team;
import com.mach.platform.api.repository.TeamRepo;
import com.mach.platform.api.repository.WxUserRepo;
import com.mach.platform.api.util.ImageUtil;
import com.mach.platform.domain.iot.IotSpace;
import com.mach.platform.repository.IotSpaceRepo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 我的团队业务处理
 */

@Service
public class TeamServiceImpl {

    @Autowired
    private TeamRepo teamRepo;

    @Autowired
    private WxUserRepo wxUserRepo;

    @Autowired
    private IotSpaceRepo iotSpaceRepo;


    /**
     * 判断是否领队
     * @param openId 用户openId
     * @param stationId 团队/站房id
     * @return 0：不是领队 1：是领队
     */
    public int isLeader(String openId, Long stationId) {
        Team team = teamRepo.checkIsLeader(openId, stationId);
        if (null == team) {
            return 0;
        }
        return 1;
    }


    /**
     * 验证用户是否已在某个团队
     * @param openId 用户openId
     * @param stationId 团队站房id
     * @return
     */
    public boolean checkIsMember(String openId, Long stationId) {
        Team team = teamRepo.findByOpenIdAndStationId(openId, stationId);
        if (null != team) {
            return true;
        }
        return false;
    }


    /**
     * 获取用户所有团队
     * @param openId 用户openId
     * @return
     */
    public List<Map> findTeamList(String openId) {
        return teamRepo.findTeamListByOpenId(openId);
    }


    /**
     * 删除团队成员
     * @param stationId 站房id
     * @param memberIds 删除的成员openId
     * @return
     */
    @Transactional(rollbackOn = Exception.class)
    public void delete(Long stationId, String[] memberIds) {
        teamRepo.deleteTeamMember(stationId, memberIds);
    }


    /**
     * 加入某个团队
     * @param openId 用户openId
     * @param stationId 团队站房id
     * @return
     */
    public void joinTeam(String openId, Long stationId) {
        IotSpace iotSpace = iotSpaceRepo.getById(stationId);
        Team team = new Team();
        team.setCreateTime(LocalDateTime.now());
        team.setOpenId(openId);
        team.setStationId(stationId);
        team.setTeamName(iotSpace.getName());
        teamRepo.save(team);
    }


    /**
     * 转让团队领队
     * @param stationId 团队站房id
     * @param leader 原领队openId
     * @param newLeader 新领队openId
     * @return
     */
    @Transactional(rollbackOn = Exception.class)
    public boolean transferLeader(Long stationId, String leader, String newLeader) {
        try {
            teamRepo.updateMember(stationId, leader);
            teamRepo.updateLeader(stationId, newLeader);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }


    /**
     * 获取团队成员信息
     * @param openId 用户openId
     * @param stationId 团队站房id
     * @return
     */
    public Map findTeamMemberInfo(String openId, Long stationId) {
        Map result = new HashMap<String, Object>();
        List<Map> memberList = wxUserRepo.findTeamMemberByStationId(stationId);
        // 组装数据
        Map leader = new HashMap();
        List<Map> members = new ArrayList<>();
        Map resultMap;
        for (Map map : memberList) {
            resultMap = new HashMap();
            resultMap.put("openId", map.get("open_id"));
            resultMap.put("nickName", map.get("nick_name"));
            String avatarPath = ImageUtil.getAvatarShowPath((String) map.get("open_id"));
            resultMap.put("avatarPath", avatarPath);
            int isLeader = (int) map.get("is_leader");
            if (isLeader == 1) {
                leader = resultMap;
            } else {
                members.add(resultMap);
            }
        }
        result.put("isLeader", isLeader(openId, stationId));
        result.put("leader", leader);
        result.put("memberList", members);
        return result;
    }


}
